<!DOCTYPE html>


<html lang="en">
  

    <head>
      <meta charset="utf-8" />
        
      <meta
        name="viewport"
        content="width=device-width, initial-scale=1, maximum-scale=1"
      />
      <title>Keepalive高可用GBase8s |  朝花夕拾</title>
  <meta name="generator" content="hexo-theme-ayer">
      
      <link rel="shortcut icon" href="/ww.ico" />
       
<link rel="stylesheet" href="/dist/main.css">

      
<link rel="stylesheet" href="/css/fonts/remixicon.css">

      
<link rel="stylesheet" href="/css/custom.css">
 
      <script src="https://cdn.staticfile.org/pace/1.2.4/pace.min.js"></script>
       
 

      <link
        rel="stylesheet"
        href="https://cdn.jsdelivr.net/npm/@sweetalert2/theme-bulma@5.0.1/bulma.min.css"
      />
      <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.0.19/dist/sweetalert2.min.js"></script>

      <!-- mermaid -->
      
      <style>
        .swal2-styled.swal2-confirm {
          font-size: 1.6rem;
        }
      </style>
    </head>
  </html>
</html>


<body>
  <div id="app">
    
      
    <main class="content on">
      <section class="outer">
  <article
  id="post-Keepalive高可用GBase8s"
  class="article article-type-post"
  itemscope
  itemprop="blogPost"
  data-scroll-reveal
>
  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  Keepalive高可用GBase8s
</h1>
 

      
    </header>
     
    <div class="article-meta">
      <a href="/2023/04/23/Keepalive%E9%AB%98%E5%8F%AF%E7%94%A8GBase8s/" class="article-date">
  <time datetime="2023-04-23T02:24:30.000Z" itemprop="datePublished">2023-04-23</time>
</a> 
  <div class="article-category">
    <a class="article-category-link" href="/categories/Develop/">Develop</a>
  </div>
  
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> Word count:</span>
            <span class="post-count">2.2k</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> Reading time≈</span>
            <span class="post-count">10 min</span>
        </span>
    </span>
</div>
 
    </div>
      
    <div class="tocbot"></div>




  
    <div class="article-entry" itemprop="articleBody">
       
  <h2 id="软件附件存储位置："><a href="#软件附件存储位置：" class="headerlink" title="软件附件存储位置："></a>软件附件存储位置：</h2><p>   本地存储位置： D:\soft\09.博客附件\KeepalivedGBase8s<br>   网络存储位置： <a target="_blank" rel="noopener" href="https://pan.baidu.com/s/1UQCZpYVxYF7pBHsTUFjmzA">点击下载</a>  提取码：6789</p>
<h2 id="部署方法"><a href="#部署方法" class="headerlink" title="部署方法"></a>部署方法</h2><p>1,  编辑 config_files.txt 填写你的环境的真实数据</p>
<p>2,  执行  .&#x2F;config_files.sh</p>
<p>3,  把 config_files 目录的文件上传到正确的为位置即可。</p>
<p>4，先启动数据库2个节点的集群，再启动主节点的keeplive进程，再启动备节点的keeplive进程。</p>
<h2 id="软件说明"><a href="#软件说明" class="headerlink" title="软件说明"></a>软件说明</h2><ul>
<li><p>功能</p>
<ol>
<li><p>提供vip</p>
</li>
<li><p>提供故障停止库和切换</p>
</li>
<li><p>提供启动时检查是否有主，有加入集群，没有则自启动为主。</p>
</li>
</ol>
</li>
<li><p>组件</p>
<ol>
<li>每2秒执行&#x2F;opt&#x2F;gbkeepalived&#x2F;gbase8s_check.sh检查GBase数据库是否可用，如果不可用，停止keeplive进程。</li>
<li>keeplive升级为主时执行&#x2F;opt&#x2F;gbkeepalived&#x2F;master.sh停止复制关系并升级为主数据库。</li>
<li>keeplive停止时执行&#x2F;opt&#x2F;gbkeepalived&#x2F;stop.sh停止数据库。</li>
<li>oninit.sh为启动脚本，启动数据库时检查集群是否有主存活，有加入集群，没有则自启动为主。并启动keeplive进程。</li>
</ol>
</li>
</ul>
<h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><pre><code>    调研GBase8s keepalive高可用解决方案，要达到两个主要目标：主库出现问题时快速自动切换到从库；对应用透明。
</code></pre>
<h2 id="原理"><a href="#原理" class="headerlink" title="原理"></a>原理</h2><p>VRRP 虚拟路由冗余协议</p>
<p>Virtual Router Redundancy Protocol</p>
<p>构建多个路由器,将几台路由器组成一个集群.</p>
<p>多个backup路由器由权重选举新的master.</p>
<p>master节点会不停的发送心跳给backup节点,当backup节点接收不到心跳,它会认为master故障了,backup选举.</p>
<h2 id="下载安装"><a href="#下载安装" class="headerlink" title="下载安装"></a>下载安装</h2><p>官网 <a target="_blank" rel="noopener" href="https://www.keepalived.org/index.html">https://www.keepalived.org/index.html</a></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">[root@h11 opt]# tar zxvf keepalived-2.2.7.tar.gz</span><br><span class="line">[root@h12 opt]# tar zxvf keepalived-2.2.7.tar.gz</span><br><span class="line"></span><br><span class="line">[root@h11 keepalived-2.2.7]# yum -y install gcc</span><br><span class="line">[root@h11 keepalived-2.2.7]# yum -y install openssl</span><br><span class="line">[root@h11 keepalived-2.2.7]# yum -y install openssl-devel</span><br><span class="line">[root@h11 opt]# cd keepalived-2.2.7</span><br><span class="line">[root@h11 keepalived-2.2.7]# ./configure --prefix=/usr/local/keepalived --sysconf=/etc</span><br><span class="line">[root@h11 keepalived-2.2.7]# make &amp;&amp; make install</span><br><span class="line">[root@h11 keepalived-2.2.7]# whereis keepalived</span><br></pre></td></tr></table></figure>



<h2 id="把keepalived注册为系统服务"><a href="#把keepalived注册为系统服务" class="headerlink" title="把keepalived注册为系统服务"></a>把keepalived注册为系统服务</h2><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">cd /opt/keepalived-2.2.7/keepalived/etc</span><br><span class="line">cp init.d/keepalived /etc/init.d/</span><br><span class="line">cp sysconfig/keepalived /etc/sysconfig/</span><br><span class="line">systemctl daemon-reload</span><br><span class="line">systemctl restart keepalived</span><br><span class="line">systemctl enable keepalived</span><br><span class="line">systemctl status keepalived</span><br></pre></td></tr></table></figure>



<h2 id="GBase8s-Keepalived-高可用部署"><a href="#GBase8s-Keepalived-高可用部署" class="headerlink" title="GBase8s+Keepalived 高可用部署"></a>GBase8s+Keepalived 高可用部署</h2><h3 id="1-环境"><a href="#1-环境" class="headerlink" title="1. 环境"></a>1. 环境</h3><figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">环境：</span></span><br><span class="line">         GBase8s Primary        192.168.1.11</span><br><span class="line">         GBase8s Secondary      192.168.1.12</span><br><span class="line">         VIP              		192.168.1.13/24</span><br><span class="line">         GBase8s主从HDR复制配置从略。</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">DR:</span></span><br><span class="line">DRAUTO                  0</span><br><span class="line">DRINTERVAL              0</span><br><span class="line">HDR_TXN_SCOPE           NEAR_SYNC</span><br><span class="line">DRTIMEOUT               3</span><br><span class="line">onmode -wf DRTIMEOUT=3</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">SQLHOSTS:</span></span><br><span class="line">g330pri onsoctcp 192.168.1.11 9083</span><br><span class="line">g330hdr onsoctcp 192.168.1.12 9083</span><br><span class="line">g330vip onsoctcp 0.0.0.0      9088</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">192.168.1.11的ONCONFIG:</span></span><br><span class="line">DBSERVERNAME g330pri</span><br><span class="line">DBSERVERALIASES g330vip</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">192.168.1.12的ONCONFIG:</span></span><br><span class="line">DBSERVERNAME g330hdr</span><br><span class="line">DBSERVERALIASES g330vip</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">sqlhosts.vip (客户端连接用)</span></span><br><span class="line">g330vip onsoctcp 192.138.1.13  9088</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>环境变量</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">if [ -f ~/.bashrc ]; then</span><br><span class="line">        . ~/.bashrc</span><br><span class="line">fi</span><br><span class="line">PATH=$PATH:$HOME/.local/bin:$HOME/bin</span><br><span class="line">export PATH</span><br><span class="line"></span><br><span class="line">export GBASEDBTDIR=/opt/install/g330</span><br><span class="line">export GBASEDBTSERVER=$server__node</span><br><span class="line">export ONCONFIG=onconfig.$&#123;GBASEDBTSERVER&#125;</span><br><span class="line">export PATH=$&#123;GBASEDBTDIR&#125;/bin:$&#123;PATH&#125;:.</span><br><span class="line">export GBASEDBTSQLHOSTS=$&#123;GBASEDBTDIR&#125;/etc/sqlhosts</span><br><span class="line">export LD_LIBRARY_PATH=$GBASEDBTDIR/lib:$GBASEDBTDIR/lib/esql:$GBASEDBTDIR/lib/cli:$GBASEDBTDIR/lib/dmi</span><br><span class="line">export DB_LOCALE=zh_CN.utf8</span><br><span class="line">export CLIENT_LOCALE=zh_CN.utf8</span><br><span class="line">export GL_USEGLU=1</span><br><span class="line">export DBDATE=&quot;Y4MD-&quot;</span><br><span class="line">export DBACCESS_SHOW_TIME=1</span><br><span class="line"></span><br><span class="line">export GBASEDBTCONTIME=1</span><br><span class="line">export GBASEDBTCONRETRY=0</span><br><span class="line">export GBASEDBTOFFSETS=1</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>ONCONFIG</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"></span><br></pre></td></tr></table></figure>





<h3 id="2-高可用配置"><a href="#2-高可用配置" class="headerlink" title="2. 高可用配置"></a>2. 高可用配置</h3><ul>
<li><p>keepalived配置文件如下：&#x2F;etc&#x2F;keepalived&#x2F;keepalived.conf</p>
<p><strong>直接复制下面框体内容,创建&#x2F;etc&#x2F;keepalived&#x2F;keepalived.conf文件,主节点100从节点90即可</strong></p>
</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br></pre></td><td class="code"><pre><span class="line">global_defs &#123;</span><br><span class="line">   router_id GBASE8S-HA</span><br><span class="line">&#125; </span><br><span class="line"></span><br><span class="line">vrrp_script check_run &#123;</span><br><span class="line">script &quot;/home/gbasedbt/gbase8s_check.sh&quot;</span><br><span class="line">interval 2</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">vrrp_sync_group VG1 &#123;</span><br><span class="line">group &#123;</span><br><span class="line">VI_1</span><br><span class="line">&#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">vrrp_instance VI_1 &#123;</span><br><span class="line">    state BACKUP</span><br><span class="line">    interface eth0 </span><br><span class="line">    virtual_router_id 51</span><br><span class="line">    #priority 100</span><br><span class="line">    priority 90  </span><br><span class="line">    advert_int 1</span><br><span class="line">    nopreempt</span><br><span class="line">    authentication &#123;</span><br><span class="line">        auth_type PASS</span><br><span class="line">        auth_pass 1234</span><br><span class="line">    &#125;</span><br><span class="line">    track_script &#123;</span><br><span class="line">    check_run</span><br><span class="line">    &#125;</span><br><span class="line">    notify_master /home/gbasedbt/master.sh</span><br><span class="line">    notify_stop /home/gbasedbt/stop.sh</span><br><span class="line"></span><br><span class="line">    virtual_ipaddress &#123;</span><br><span class="line">        192.168.1.13/24</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<pre><code>    master与slave的keepalived配置文件中只有priority设置不同，master为100，slave为90，其它全一样。nopreempt参数是priority大的后启动,不会去抢占master.
</code></pre>
<ul>
<li><p>&#x2F;home&#x2F;gbasedbt&#x2F;gbase8s_check.sh文件用以检测GBase8s服务是否正常，当发现连接不上gbase8s，自动把keepalived进程杀掉，让VIP进行漂移。文件内容如下。</p>
<p><strong>直接复制下面框体内容,创建&#x2F;home&#x2F;gbasedbt&#x2F;gbase8s_check.sh文件,并給755权限即可</strong></p>
</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line">. /home/gbasedbt/.bash_profile</span><br><span class="line">export GBASEDBTCONTIME=1</span><br><span class="line">export GBASEDBTCONRETRY=0</span><br><span class="line">export GBASEDBTOFFSETS=1</span><br><span class="line">count=1</span><br><span class="line"></span><br><span class="line">while true</span><br><span class="line">do</span><br><span class="line"></span><br><span class="line">i=$(echo &#x27;select 123456654321 from sysdual;&#x27; |dbaccess sysmaster 2&gt;/dev/null |grep 123456654321 |wc -l)</span><br><span class="line">j=$(onstat - |grep &#x27;not initialized&#x27; |wc -l)</span><br><span class="line"></span><br><span class="line">if [ $i = 1 ] &amp;&amp; [ $j = 0 ]</span><br><span class="line">then</span><br><span class="line">   exit 0</span><br><span class="line">else</span><br><span class="line">   if [ $i = 0 ] &amp;&amp; [ $j = 0 ]</span><br><span class="line">   then </span><br><span class="line">   	  exit 0</span><br><span class="line">   else</span><br><span class="line">   		if [ $count -gt 5 ]</span><br><span class="line">   			then</span><br><span class="line">      			break</span><br><span class="line">   		fi</span><br><span class="line">   		let count++</span><br><span class="line">   		continue</span><br><span class="line">   fi</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">done</span><br><span class="line"></span><br><span class="line">/etc/init.d/keepalived stop</span><br><span class="line"></span><br></pre></td></tr></table></figure>





<ul>
<li><p>&#x2F;home&#x2F;gbasedbt&#x2F;master.sh的作用是状态改为master以后执行的脚本。首先判断复制是否有延迟，如果有日志延迟，等1分钟后，不论是否有延迟，都停止复制， 并自立为主。文件内容如下。</p>
<p><strong>直接复制下面框体内容,创建&#x2F;home&#x2F;gbasedbt&#x2F;master.sh文件,并給755权限即可</strong></p>
</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line">. /home/gbasedbt/.bash_profile</span><br><span class="line"></span><br><span class="line">i=1</span><br><span class="line"></span><br><span class="line">while true</span><br><span class="line">do</span><br><span class="line"></span><br><span class="line">ID1=$(onstat -l |grep C|awk &#x27;&#123;print $4&#125;&#x27;)</span><br><span class="line">POS1=$(onstat -l |grep C|awk &#x27;&#123;print $7&#125;&#x27;)</span><br><span class="line">sleep 3</span><br><span class="line">ID2=$(onstat -l |grep C|awk &#x27;&#123;print $4&#125;&#x27;)</span><br><span class="line">POS2=$(onstat -l |grep C|awk &#x27;&#123;print $7&#125;&#x27;)</span><br><span class="line"></span><br><span class="line">if [ $ID1 = $ID2 ] &amp;&amp; [ $POS1 = $POS2 ]</span><br><span class="line">then</span><br><span class="line">   echo &quot;ok&quot;</span><br><span class="line">   break</span><br><span class="line">else</span><br><span class="line">   if [ $i -gt 20 ]</span><br><span class="line">   then</span><br><span class="line">      break</span><br><span class="line">   fi</span><br><span class="line">   let i++</span><br><span class="line">   continue</span><br><span class="line">fi</span><br><span class="line">done</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">停止复制关系</span></span><br><span class="line">while true</span><br><span class="line">do</span><br><span class="line">	sleep 1</span><br><span class="line">	su - gbasedbt -c &quot;onmode -d standard&quot;</span><br><span class="line"><span class="meta prompt_">	#</span><span class="language-bash">su - gbasedbt -c <span class="string">&quot;onmode -d make primary <span class="variable">$GBASEDBTSERVER</span> force&quot;</span></span></span><br><span class="line">	if  [ $? = 0 ]</span><br><span class="line">	then</span><br><span class="line">		break</span><br><span class="line">	fi</span><br><span class="line">done</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">切换新主</span></span><br><span class="line">objectServer=$(cat $GBASEDBTDIR/etc/sqlhosts |grep -vE &quot;$GBASEDBTSERVER|g330vip&quot;|awk &#x27;&#123;print $1&#125;&#x27;)</span><br><span class="line">su - gbasedbt -c &quot;onmode -d primary $objectServer&quot;</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">记录当前状态</span></span><br><span class="line">echo &quot;$(date &#x27;+%y%m%d-%H%M&#x27;) $GBASEDBTSERVER up to Primary.&quot; &gt;&gt; /tmp/online.keeplive.log</span><br><span class="line"></span><br></pre></td></tr></table></figure>



<ul>
<li>&#x2F;home&#x2F;gbasedbt&#x2F;stop.sh 当keepalived停止的时候把数据库也停下来</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line">. /home/gbasedbt/.bash_profile</span><br><span class="line"></span><br><span class="line">echo &quot;$(date &#x27;+%y%m%d-%H%M&#x27;) Keepalived Stoped.&quot; &gt;&gt; /tmp/online.keeplive.log</span><br><span class="line">su - gbasedbt -c &quot;onclean -ky&quot;</span><br><span class="line">systemctl stop gbasedbtd</span><br><span class="line">echo &quot;$(date &#x27;+%y%m%d-%H%M&#x27;) GBase8s Stoped.&quot; &gt;&gt; /tmp/online.keeplive.log</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>






<ul>
<li><p>crt_gbasedbtd.sh 把gbase8s配置成服务,开机自启动数据库,并再次启动Keepalived服务.关机时自关闭数据库.</p>
<p>说明:启动数据库的时候,如果自己原来是主机,但是现在对端已经抢占了主身份,启动会失败,这时候用PHY启动并加入集群.</p>
<p><strong>直接复制下面框体内容,然后root用户粘贴即可.</strong></p>
</li>
</ul>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">gbasedbtd服务环境基础</span></span><br><span class="line">. /home/gbasedbt/.bash_profile</span><br><span class="line">mkdir -p /etc/sysconfig</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">gbasedbtd服务环境</span></span><br><span class="line">if [ $GBASEDBTSQLHOSTS ] ; then</span><br><span class="line">	SQLHOSTS=$GBASEDBTSQLHOSTS</span><br><span class="line">else</span><br><span class="line">	SQLHOSTS=$GBASEDBTDIR/etc/sqlhosts</span><br><span class="line">fi</span><br><span class="line">cat &gt; /etc/sysconfig/gbasedbtd_service &lt;&lt; EOF</span><br><span class="line">GBASEDBTDIR=$GBASEDBTDIR</span><br><span class="line">GBASEDBTSERVER=$GBASEDBTSERVER</span><br><span class="line">ONCONFIG=$ONCONFIG</span><br><span class="line">GBASEDBTSQLHOSTS=$SQLHOSTS</span><br><span class="line">EOF</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">gbasedbtd服务</span></span><br><span class="line">cat &gt; /usr/lib/systemd/system/gbasedbtd.service &lt;&lt; EOF</span><br><span class="line">[Unit]</span><br><span class="line">Description=GBase 8s Database Server</span><br><span class="line">Documentation=file:$GBASEDBTDIR/release/en_us/0333</span><br><span class="line">Wants=network-online.target</span><br><span class="line">After=network.target network-online.target</span><br><span class="line"></span><br><span class="line">[Service]</span><br><span class="line">Type=oneshot</span><br><span class="line">User=root</span><br><span class="line">Group=root</span><br><span class="line">RemainAfterExit=yes</span><br><span class="line">EnvironmentFile=/etc/sysconfig/gbasedbtd_service</span><br><span class="line">ExecStart=$GBASEDBTDIR/bin/oninit.sh</span><br><span class="line">ExecStop=$GBASEDBTDIR/bin/onmode -ky</span><br><span class="line"><span class="meta prompt_">#</span><span class="language-bash">Restart=on-abort</span></span><br><span class="line"></span><br><span class="line">[Install]</span><br><span class="line">WantedBy=multi-user.target</span><br><span class="line">EOF</span><br><span class="line"></span><br><span class="line">systemctl daemon-reload</span><br><span class="line">systemctl enable gbasedbtd</span><br><span class="line">systemctl stop gbasedbtd</span><br><span class="line">systemctl start gbasedbtd</span><br><span class="line">systemctl status gbasedbtd</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>



<p>$GBASEDBTDIR&#x2F;bin&#x2F;oninit.sh </p>
<p><strong>直接复制下面框体内容,然后创建oninit.sh文件即可,给予755权限.</strong></p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">!/bin/bash</span></span><br><span class="line"></span><br><span class="line">cat /home/gbasedbt/.bash_profile  &gt; /tmp/bash_profile.tmp</span><br><span class="line">echo &quot;export GBASEDBTCONTIME=1&quot;  &gt;&gt; /tmp/bash_profile.tmp</span><br><span class="line">echo &quot;export GBASEDBTCONRETRY=0&quot; &gt;&gt; /tmp/bash_profile.tmp</span><br><span class="line">echo &quot;export GBASEDBTOFFSETS=1&quot;  &gt;&gt; /tmp/bash_profile.tmp</span><br><span class="line">echo &quot;export GBASEDBTSQLHOSTS=/tmp/sqlhsots.tmp&quot;  &gt;&gt; /tmp/bash_profile.tmp</span><br><span class="line">chmod 755 /tmp/bash_profile.tmp</span><br><span class="line"></span><br><span class="line">echo &quot;g330vip onsoctcp 192.168.1.13  9088&quot; &gt; /tmp/sqlhsots.tmp</span><br><span class="line"></span><br><span class="line">result=1</span><br><span class="line"></span><br><span class="line">objectServer=$(cat $GBASEDBTDIR/etc/sqlhosts |grep -vE &quot;$GBASEDBTSERVER|g330vip&quot;|awk &#x27;&#123;print $1&#125;&#x27;)</span><br><span class="line"></span><br><span class="line">vipdb=$(su - gbasedbt -c &quot;. /tmp/bash_profile.tmp &amp;&amp; echo &#x27;select 123456654321 from sysdual;&#x27; |dbaccess sysmaster@g330vip 2&gt;/dev/null |grep 123456654321 |wc -l&quot;)</span><br><span class="line"></span><br><span class="line">echo $vipdb</span><br><span class="line">if [ $vipdb -ne 1 ];then </span><br><span class="line">	su - gbasedbt -c &quot;onclean -ky&quot;</span><br><span class="line">	su - gbasedbt -c &quot;oninit -y&quot;</span><br><span class="line">	su - gbasedbt -c &quot;onmode -d primary $objectServer&quot;</span><br><span class="line">	result=$?</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">if [ $vipdb -eq 1 ];then</span><br><span class="line">	su - gbasedbt -c &quot;onclean -ky&quot;</span><br><span class="line">	su - gbasedbt -c &quot;oninit -PHY&quot;</span><br><span class="line">	su - gbasedbt -c &quot;onmode -d secondary $objectServer&quot;</span><br><span class="line">	result=$?</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">if [ $result -eq 0 ];then</span><br><span class="line">	/etc/init.d/keepalived start</span><br><span class="line">	result=$?</span><br><span class="line">fi</span><br><span class="line"></span><br><span class="line">exit $result</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>





<h2 id="关于GBase8s数据库的高可用方案小结"><a href="#关于GBase8s数据库的高可用方案小结" class="headerlink" title="关于GBase8s数据库的高可用方案小结"></a>关于GBase8s数据库的高可用方案小结</h2><ul>
<li><pre><code>    方案一: 数据库DRAUTO参数设置为0,用操作系统HA和共享存储RAID阵列保障数据库主节点高可用.从库只用作数据同步和查询使用.优点金融级别成熟可靠.缺点贵.优点故障的服务器不需要人工修复数据库。
</code></pre>
</li>
<li><pre><code>    方案二: 数据库DRAUTO参数设置为1或者2,业务用dbgroup组连接数据库集群组(group永远会透明连接到组中的主节点). 缺点主备间断网双主.缺点需要人工修复故障的服务器。
</code></pre>
</li>
<li><pre><code>    方案三: 数据库DRAUTO参数设置为3, 引入数据库的cm组件,通过连接管理器转发或代理连接.优点可以做透明的读写分离.缺点节点增多稳定性不够. 中性主备间断网数据同步中断,主和cm间断网双主.缺点需要人工修复故障的服务器。
</code></pre>
</li>
<li><pre><code>    方案四: 数据库DRAUTO参数设置为0, 引入keepalive组建,通过keepalive脚本检测数据库状态,提供数据库角色切换和vip切换.优点可以定制,有VIP. 缺点不如方案二简洁,缺点需要人工修复故障的服务器。
</code></pre>
</li>
</ul>
<h2 id="人工恢复故障节点"><a href="#人工恢复故障节点" class="headerlink" title="人工恢复故障节点"></a>人工恢复故障节点</h2><p> ontape.sh 恢复故障节点的数据,并将故障节点作为从库加入集群.在新主上执行入下命令:</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta prompt_">#</span><span class="language-bash">在主机上用gbasedbt用户和执行下面两条命令: 192.168.1.12为故障节点的IP地址</span></span><br><span class="line">su - gbasedbt</span><br><span class="line"></span><br><span class="line">ontape -s -L 0 -t STDIO -F |ssh 192.168.1.12 &quot;. /home/gbasedbt/.bash_profile; onmode -ky;onclean -ky;ontape -p -t STDIO&quot;</span><br><span class="line"></span><br><span class="line">onmode -d primary g330hdr</span><br><span class="line"></span><br><span class="line">ssh 192.168.1.12 &quot;. /home/gbasedbt/.bash_profile; onmode -d secondary g330pri&quot;</span><br><span class="line"><span class="meta prompt_"></span></span><br><span class="line"><span class="meta prompt_"># </span><span class="language-bash">GBase123!</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure> 
      <!-- reward -->
      
    </div>
    

    <!-- copyright -->
    
    <div class="declare">
      <ul class="post-copyright">
        <li>
          <i class="ri-copyright-line"></i>
          <strong>Copyright： </strong>
          
          Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
          
        </li>
      </ul>
    </div>
    
    <footer class="article-footer">
       
<div class="share-btn">
      <span class="share-sns share-outer">
        <i class="ri-share-forward-line"></i>
        分享
      </span>
      <div class="share-wrap">
        <i class="arrow"></i>
        <div class="share-icons">
          
          <a class="weibo share-sns" href="javascript:;" data-type="weibo">
            <i class="ri-weibo-fill"></i>
          </a>
          <a class="weixin share-sns wxFab" href="javascript:;" data-type="weixin">
            <i class="ri-wechat-fill"></i>
          </a>
          <a class="qq share-sns" href="javascript:;" data-type="qq">
            <i class="ri-qq-fill"></i>
          </a>
          <a class="douban share-sns" href="javascript:;" data-type="douban">
            <i class="ri-douban-line"></i>
          </a>
          <!-- <a class="qzone share-sns" href="javascript:;" data-type="qzone">
            <i class="icon icon-qzone"></i>
          </a> -->
          
          <a class="facebook share-sns" href="javascript:;" data-type="facebook">
            <i class="ri-facebook-circle-fill"></i>
          </a>
          <a class="twitter share-sns" href="javascript:;" data-type="twitter">
            <i class="ri-twitter-fill"></i>
          </a>
          <a class="google share-sns" href="javascript:;" data-type="google">
            <i class="ri-google-fill"></i>
          </a>
        </div>
      </div>
</div>

<div class="wx-share-modal">
    <a class="modal-close" href="javascript:;"><i class="ri-close-circle-line"></i></a>
    <p>扫一扫，分享到微信</p>
    <div class="wx-qrcode">
      <img src="//api.qrserver.com/v1/create-qr-code/?size=150x150&data=http://example.com/2023/04/23/Keepalive%E9%AB%98%E5%8F%AF%E7%94%A8GBase8s/" alt="微信分享二维码">
    </div>
</div>

<div id="share-mask"></div>  
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/keeplived/" rel="tag">keeplived</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E8%87%AA%E7%A0%94/" rel="tag">自研</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E9%9B%86%E7%BE%A4/" rel="tag">集群</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/%E9%AB%98%E5%8F%AF%E7%94%A8/" rel="tag">高可用</a></li></ul>

    </footer>
  </div>

   
  <nav class="article-nav">
    
      <a href="/2023/04/24/%E9%94%81%E8%A1%A8%E6%95%85%E9%9A%9C%E6%97%B6%E6%8A%93%E5%8F%96%E9%94%81%E7%9B%B8%E5%85%B3%E4%BF%A1%E6%81%AF/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            锁表故障时抓取锁相关信息
          
        </div>
      </a>
    
    
      <a href="/2023/04/21/%E5%A6%82%E4%BD%95%E5%9C%A8Ubuntu20-04%E4%B8%8A%E5%AE%89%E8%A3%85KVM/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">如何在Ubuntu20.04上安装KVM</div>
      </a>
    
  </nav>

  
   
  
    
</article>

</section>
      <footer class="footer">
  <div class="outer">
    <ul>
      <li>
        Copyrights &copy;
        2023-2024
        <i class="ri-heart-fill heart_icon"></i> Wang Wei
      </li>
    </ul>
    <ul>
      <li>
        
      </li>
    </ul>
    <ul>
      <li>
        
        
        <span>
  <span><i class="ri-user-3-fill"></i>Visitors:<span id="busuanzi_value_site_uv"></span></span>
  <span class="division">|</span>
  <span><i class="ri-eye-fill"></i>Views:<span id="busuanzi_value_page_pv"></span></span>
</span>
        
      </li>
    </ul>
    <ul>
      
    </ul>
    <ul>
      
    </ul>
    <ul>
      <li>
        <!-- cnzz统计 -->
        
      </li>
    </ul>
  </div>
</footer>    
    </main>
    <div class="float_btns">
      <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>

    </div>
    <aside class="sidebar on">
      <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ww.ico" alt="朝花夕拾"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
    </aside>
    <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
    
<script src="/js/jquery-3.6.0.min.js"></script>
 
<script src="/js/lazyload.min.js"></script>

<!-- Tocbot -->
 
<script src="/js/tocbot.min.js"></script>

<script>
  tocbot.init({
    tocSelector: ".tocbot",
    contentSelector: ".article-entry",
    headingSelector: "h1, h2, h3, h4, h5, h6",
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer: "main",
    positionFixedSelector: ".tocbot",
    positionFixedClass: "is-position-fixed",
    fixedSidebarOffset: "auto",
  });
</script>

<script src="https://cdn.staticfile.org/jquery-modal/0.9.2/jquery.modal.min.js"></script>
<link
  rel="stylesheet"
  href="https://cdn.staticfile.org/jquery-modal/0.9.2/jquery.modal.min.css"
/>
<script src="https://cdn.staticfile.org/justifiedGallery/3.8.1/js/jquery.justifiedGallery.min.js"></script>

<script src="/dist/main.js"></script>

<!-- ImageViewer -->

<!-- MathJax -->

<!-- Katex -->

<!-- busuanzi  -->
 
<script src="/js/busuanzi-2.3.pure.min.js"></script>
 
<!-- ClickLove -->

<!-- ClickBoom1 -->

<!-- ClickBoom2 -->

<!-- CodeCopy -->
 
<link rel="stylesheet" href="/css/clipboard.css">
 <script src="https://cdn.staticfile.org/clipboard.js/2.0.10/clipboard.min.js"></script>
<script>
  function wait(callback, seconds) {
    var timelag = null;
    timelag = window.setTimeout(callback, seconds);
  }
  !function (e, t, a) {
    var initCopyCode = function(){
      var copyHtml = '';
      copyHtml += '<button class="btn-copy" data-clipboard-snippet="">';
      copyHtml += '<i class="ri-file-copy-2-line"></i><span>COPY</span>';
      copyHtml += '</button>';
      $(".highlight .code pre").before(copyHtml);
      $(".article pre code").before(copyHtml);
      var clipboard = new ClipboardJS('.btn-copy', {
        target: function(trigger) {
          return trigger.nextElementSibling;
        }
      });
      clipboard.on('success', function(e) {
        let $btn = $(e.trigger);
        $btn.addClass('copied');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-checkbox-circle-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPIED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-checkbox-circle-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
      clipboard.on('error', function(e) {
        e.clearSelection();
        let $btn = $(e.trigger);
        $btn.addClass('copy-failed');
        let $icon = $($btn.find('i'));
        $icon.removeClass('ri-file-copy-2-line');
        $icon.addClass('ri-time-line');
        let $span = $($btn.find('span'));
        $span[0].innerText = 'COPY FAILED';
        
        wait(function () { // 等待两秒钟后恢复
          $icon.removeClass('ri-time-line');
          $icon.addClass('ri-file-copy-2-line');
          $span[0].innerText = 'COPY';
        }, 2000);
      });
    }
    initCopyCode();
  }(window, document);
</script>
 
<!-- CanvasBackground -->

<script>
  if (window.mermaid) {
    mermaid.initialize({ theme: "forest" });
  }
</script>


    
    

  </div>
</body>

</html>